package com.ty.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.annotation.Auth;
import com.ty.entity.UserAddress;
import com.ty.resp.CommonResp;
import com.ty.service.IUserAddressService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户地址表 前端控制器
 * </p>
 *
 * @author txy
 * @since 2024-02-04
 */
@RestController
@RequestMapping("/userAddress")
@Auth(roles = "admin,user")
public class UserAddressController {
    @Resource
    private IUserAddressService addressService;

    @PostMapping("/addAddress")
    private CommonResp addAddress(UserAddress userAddress) {
        CommonResp resp = new CommonResp();
        boolean save = addressService.save(userAddress);
        resp.setSuccess(save);
        if (!save) {
            resp.setMessage("服务器内部出现问题");
        }
        return resp;
    }

    @GetMapping("/getAddress")
    public CommonResp getAddressByUserId(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "2") Integer size,
                                     String userId, String keyWord) {
        return addressService.getAddressByUserId(new Page<>(page, size), userId, keyWord);
    }
    @GetMapping("/getAddressById")
    public CommonResp getAddressById(String addrId, String userId) {
        return addressService.getAddressById(addrId,userId);
    }

    @PostMapping("/updateAddressById")
    public CommonResp updateAddressById(UserAddress userAddress) {
        CommonResp resp = new CommonResp();
        if (userAddress.getId() == null) {
            resp.setSuccess(false);
            resp.setMessage("地址的编号id没提交");
        } else {
            boolean update = addressService.updateById(userAddress);
            if (!update) {
                resp.setSuccess(false);
                resp.setMessage("服务器修改是失败");
            }
        }
        return resp;
    }

    @PostMapping("/deleteAddressById")
    public CommonResp deleteAddressById(String addrId) {
        return addressService.deleteAddressById(addrId);
    }
}
